const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

//输出目录名
const outputDirName = 'dist';

module.exports = {
    entry: './src/index.jsx',
    output: {
        path: path.resolve(__dirname, outputDirName),
        publicPath: '/dist/',
        // [hash]。hash值是特定于整个构建过程的。
        // [chunkhash]。hash值是特定于每一个文件的内容的。
        filename: '[name].[hash].js',
    },
    module: {
        rules: [{
                test: /\.(js|jsx)$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ["env", "react"]
                    }
                }
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader', 'sass-loader']
                })
            },
            {
                test: /\.(png|jpg|gif|jpeg)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 8192,
                        // publicPath表示打包文件中引用文件的路径前缀，如果你的图片存放在CDN上，那么你上线时可以加上这个参数，值为CDN地址，这样就可以让项目上线后的资源引用路径指向CDN了。
                        // publicPath: "http://localhost:1022/assets/",
                        outputPath: "img/",
                    }
                }]
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2|otf)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 8192,
                        name: 'resource/[name].[ext]'
                    }
                }]
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin([outputDirName]), //将css打包到css link 
        new HtmlWebpackPlugin({
            // title: "MyApp", //设置title
            // filename: "index.html", //设置文件名，default -> index.html
            template: "./src/index.html"
        }),
        new ExtractTextPlugin("index.[hash].css"),
    ],
    devServer: {
        port: 8086,
    }
};